package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.fizy.client.objects.Track;

public class Sample {
	final String END_POINT = "https://api.fizy.com";

	public Track getTracks() throws Exception {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		// HttpGet getRequest = new HttpGet("https://api.fizy.com/v1/moods");
		HttpGet getRequest = new HttpGet(END_POINT + "/v1/tracks/1");
		getRequest.addHeader("accept", "application/json");
		String credentials = "test" + ":" + "k7t7Yk..RsadRWzzV?vkhkHFNrR";
		String encodedAuth = "Basic " + new String(Base64.encodeBase64(credentials.getBytes("UTF-8")), "UTF-8");
		getRequest.addHeader("Authorization", encodedAuth);
		HttpResponse response = httpClient.execute(getRequest);

		//		HttpPost postRequest = new HttpPost(END_POINT);
		//		postRequest.addHeader("accept", "application/json");
		//		String credentials = "test" + ":" + "k7t7Yk..RsadRWzzV?vkhkHFNrR";
		//		String encodedAuth = "Basic " + new String(Base64.encodeBase64(credentials.getBytes("UTF-8")), "UTF-8");
		//		postRequest.addHeader("Authorization", encodedAuth);
		//		String input = "{\"v1\":\"tracks\",\"title\":\"Fade To Black\"}";
		//		StringEntity input = new StringEntity("v1/tracks/1");
		//		postRequest.setEntity(input);
		//		HttpResponse response = httpClient.execute(postRequest);

		if (response.getStatusLine().getStatusCode() != 200) {
			throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
		}

		BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

		StringBuilder sb = new StringBuilder();
		String line;
		try {
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		String output = sb.toString();
		System.out.println("Output from Server : " + output);
		JSONParser jsonParser = new JSONParser();
		JSONObject jsonObject = (JSONObject) jsonParser.parse(output);

		Object n = jsonObject.get("track");
		System.out.println(n);

		ObjectMapper mapper = new ObjectMapper();
		Track track = null;
		try {
			track = mapper.readValue(n.toString(), Track.class);
			System.out.println(track);
		} catch (JsonGenerationException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		httpClient.getConnectionManager().shutdown();
		return track;
	}
}
